import { commonTypeList } from "@/assets/deviceTypes/types.js";
import axios from "axios";
import { baseURL } from "@/utils/request";
import { modelGetListService, fileDownLoadService } from "@/api/model.js";

// 加载模型
export function initModelData(factoryId, commonFun, pipeFun, labelFun, selfFun) {
    return new Promise(async (resolve) => {
        const res = await modelGetListService({ factoryId });
        const { commonModels, selfModels, pipes, labels } = res.data.data;
        // 系统模型
        for (let i = 0; i < commonModels.length; i++) {
            const {
                commonTypeId,
                transformParam: {
                    positionX,
                    positionY,
                    positionZ,
                    rotationX,
                    rotationY,
                    rotationZ,
                    scaleX,
                    scaleY,
                    scaleZ,
                },
            } = commonModels[i];
            const param = {
                commonTypeId,
                path: getPathById(commonTypeId),
                position: [positionX, positionY, positionZ],
                rotation: [rotationX, rotationY, rotationZ],
                scale: [scaleX, scaleY, scaleZ],
            };
            commonFun(param)
        }
        // 管道
        for (let i = 0; i < pipes.length; i++) {
            pipeFun(pipes[i].points)
        }
        // 标签
        for (let i = 0; i < labels.length; i++) {
            let tag = labels[i];
            labelFun(tag)
        }
        // 自定义模型
        for (let i = 0; i < selfModels.length; i++) {
            try {
                const path = await downloadFile(
                    selfModels[i].fileId,
                    selfModels[i].filename
                );
                const {
                    positionX,
                    positionY,
                    positionZ,
                    rotationX,
                    rotationY,
                    rotationZ,
                    scaleX,
                    scaleY,
                    scaleZ,
                } = selfModels[i].transformParam;
                const param = {
                    path,
                    position: [positionX, positionY, positionZ],
                    rotation: [rotationX, rotationY, rotationZ],
                    scale: [scaleX, scaleY, scaleZ],
                };
                selfFun(param, selfModels[i].fileId, selfModels[i].filename)
            } catch (err) {
                console.log(err);
                ElMessage.error("文件加载时发生错误");
            }
        }

        resolve(true)
    })

};

function downloadFile(fileId, filename) {
    return new Promise(async (resolve) => {
        // 请求得到blob
        const res = await axios.get(baseURL + "/model/download", {
            params: { fileId },
            responseType: "blob",
        });
        // 将下载的数据转换成 Blob 对象
        const blob = new Blob([res.data], {
            type: res.headers["content-type"],
        });
        // 创建 File 对象
        const file = new File([blob], filename, {
            type: res.headers["content-type"],
        });
        const filePath = URL.createObjectURL(file);
        resolve(filePath);
    })
}

function getPathById(id) {
    let path;
    commonTypeList.some((item) => {
        if (item.id === id) {
            path = item.path;
            return true;
        }
    });
    return path;
};